<template slot="title">
  <el-dialog title="复用评分规则" :close-on-click-modal="false" :visible.sync="visible" width="800px">
    <el-container>
      <el-main style="max-height:60vh">
        <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
          <el-form-item label="规则名称" prop="gzName" :rules="[
            { required: true, message: '规则名称不能为空', trigger: 'blur' },
            { min:1, max:200, message: '长度在 1 到 200 个字符', trigger: 'blur' }]">
            <el-input v-model="dataForm.gzName" placeholder="规则名称" style='width:90%;'></el-input>
          </el-form-item>
          <el-form-item label="应用范围" prop="yyFw" :rules="[{ required: true, message: '应用范围不能为空', trigger: 'blur' }]">
            <el-select v-model="dataForm.yyFw" style='width:90%;'>
              <el-option v-for="(item,index) in ['个性','通用']" :key="index" :label="item" :value="item"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
    <span slot="footer" class="dialog-footer">

      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/**
 * 绩效考核_测评_评分规则表单复用
 * @author zmk
 * @date 2021-07-04 10:06:44
 */
import { getObj } from '@/api/ceping/cepinggzxxb';
import { postCoybRule } from '@/api/ceping/cepinggzxxb';

export default {
  data() {
    return {
      visible: false,
      canSubmit: false,
      dataForm: {
        gzName: '',
        yyFw: ''
      },
      id: 0,
    };
  },
  methods: {
    init(id) {
      this.id = id;
      this.visible = true;
      this.canSubmit = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields();
        if (id) {
          this.dataForm.dataList = [];
          getObj(id).then(response => {
            this.dataForm = response.data.data;
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;

          postCoybRule(this.id,this.dataForm.gzName,this.dataForm.yyFw).then(data => {
            this.$notify.closeAll();
            this.$notify.success('复用成功');
            this.visible = false;
            this.$emit('refreshDataList');
          }).catch(() => {
            this.canSubmit = true;
            this.$notify.closeAll();
            this.$notify.error("复用失败");
          });
        }
      });
    },
  }
};

</script>
